<?php


namespace app\admin\controller;


use app\BaseController;
use think\facade\View;
use think\auth\Auth;
use app\common\model\mysql\AdminTrade;
class Mall extends BaseController
{
    private $auth;
    private $admin_trade;
    public function __construct()
    {
        $this->auth = Auth::instance();
        $this->admin_trade = new AdminTrade;
    }

    public function addMall()
    {
        if ($this->auth->check('Mall/addMall', session(config("session.session_admin"))['id'])){
            return View::fetch('Mall/increase');
         }
    }

    public function getDataAllMall()
    {
        if ($this->auth->check('Mall/addMall', session(config("session.session_admin"))['id'])) {
            return View::fetch('Mall/commodityAdd');
        }
    }

    public function addCommodity()
    {
        if ($this->auth->check('Mall/addMall', session(config("session.session_admin"))['id'])) {
            //是否为post请求访问
            if (!request()->isPost()) {
                return show(config("status.error"), '请求方式错误');
            }

            $files = request()->file('file');
            $data = input('param.mes');

            if (empty($files)) {
                return show(config("status.error"), '图片未添加');
            }

            foreach ($files as $file) {
                $savename[] = \think\facade\Filesystem::disk('public')->putFile('topic', $file);
            }

            try{
                $result =  $this->admin_trade->addSave($savename, $data,session(config("session.session_admin"))['username']);
                if(empty($result)){
                    return show(config("status.error"),'未知异常');
                }
            }catch (\Exception $e){
                return show (config("status.error"),$e->getMessage());
            }

            return show(config("status.success"),'添加成功');
        }
    }

    public function addrichtext()
    {
        if ($this->auth->check('Mall/addMall', session(config("session.session_admin"))['id'])) {
            if (!request()->isPost()) {
                return show(config("status.error"), '请求方式错误');
            }

            $files = request()->file('file');
            $savename = \think\facade\Filesystem::disk('public')->putFile('richtext', $files);

            $result = [
                'code' => 0,
                'data' => [
                    'src' => '/storage/' . $savename,
                ],
            ];

            return json($result);
        }
    }

    public function getTrade()
    {
        if ($this->auth->check('Mall/addMall', session(config("session.session_admin"))['id'])) {
            $rows = input('limit') ?? 10;
            $page = input('page') ?? 1;
            $page -= 1;
            $offset = ($page) * $rows;
            $result = array();
            $result['code'] = 0;
            $result['msg'] = '';
            $result['count'] = $this->admin_trade->where('name', 'like', '%' . input('name') . '%')->count();
            $result_temp = $this->admin_trade->field('id,name,money,performer,status,update_time,create_time')->where('name', 'like', '%' . input('name') . '%')->limit($offset, $rows)->select()->toArray();
            $result['data'] = $result_temp;
            return json($result);
        }
    }

    public function updateTrade()
    {
        if ($this->auth->check('Mall/addMall',session(config("session.session_admin"))['id'])) {
            $id = request()->param('id');
            $field = request()->param('field');
            $value = request()->param('value');
            return $this->admin_trade->updateTrade($id,$field,$value,session(config("session.session_admin"))['username']);
        }
    }

    public function delTrade()
    {
        if ($this->auth->check('Mall/addMall',session(config("session.session_admin"))['id'])) {
            $id = request()->param('id');
            $result = $this->admin_trade->destroy($id);
            if(empty($result)){
                return json(['code' => 1,'msg'=>'未知异常']);
            }
            return json(['code' => 0,'msg'=>'删除成功']);
        }
    }

}